﻿Imports System.Windows.Browser
Imports System.ComponentModel
Imports System.ServiceModel

Partial Public Class Page
  Inherits UserControl

  Public Sub New()
    InitializeComponent()
  End Sub

  Dim WsService As SrvWCF.ServiceWCFClient
  Dim WSBinding As BasicHttpBinding
  Dim WsEnpoint As EndpointAddress
  Dim BaseAddress As String

  Private Sub Page_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
    Try

      BaseAddress = "http://localhost:2298/ServiceWCF.svc"
      txAddress.Text = BaseAddress

      chkAddress.IsChecked = False

      txAddress.IsEnabled = False
      txAddress.IsReadOnly = True

      Dim sTitle As String = String.Format("document.title=document.title + ' ' + '{0}';", GetVersion).ToString

      HtmlPage.Window.Eval(sTitle)

      UpdateStatus("Loaded")

    Catch ex As Exception
      txMsg.Text = ex.ToString
    End Try
  End Sub

  Private Sub UpdateStatus(ByVal Smsg As String)
    Try
      txStatus.Text = Smsg
      txWhen.Text = Now.ToLongTimeString
    Catch ex As Exception
      txMsg.Text = ex.ToString
    End Try
  End Sub



  Public Function GetVersion() As String
    Dim result As String = ""
    Try
      Dim asm As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly
      result = Split(asm.FullName, ",")(1)
    Catch ex As Exception
    End Try

    Return (result)

  End Function

#Region "WS Management"

  Private Sub InitializeWs(ByRef WSGen As SrvWCF.ServiceWCFClient)

    Try

      WSBinding = New BasicHttpBinding
      With WSBinding
        .MaxBufferSize = Int32.MaxValue
        .MaxReceivedMessageSize = Int32.MaxValue
      End With

      WsEnpoint = New EndpointAddress(New Uri(BaseAddress))

      WSGen = New SrvWCF.ServiceWCFClient(WSBinding, WsEnpoint)

      AddHandler WSGen.GetDataCompleted, AddressOf WsGetDataCompleted
      AddHandler WSGen.TestCompleted, AddressOf WsTestCompleted

      UpdateStatus("Ws initialized")

    Catch ex As Exception
      txMsg.Text = ex.ToString
    End Try


  End Sub

  Private Sub WsTestCompleted(ByVal sender As Object, ByVal e As SrvWCF.TestCompletedEventArgs)
    Try

      If e Is Nothing Then
        txMsg.Text = "Error Receiving WFC Answer"
        Exit Try
      End If

      If e.Result.Err.Length > 0 Then
        txMsg.Text = String.Format("Error WFC: {0}", e.Result.Err)
        Exit Try
      End If


      txMsg.Text = e.Result.Message

      UpdateStatus("Ws GetData Completed")

    Catch ex As Exception
      txMsg.Text = ex.ToString
    End Try
  End Sub

  Private Sub WsGetDataCompleted(ByVal sender As Object, ByVal e As SrvWCF.GetDataCompletedEventArgs)
    Try
      If e Is Nothing Then
        txMsg.Text = "Error Receiving WFC Answer"
        Exit Try
      End If

      If e.Result.Err.Length > 0 Then
        txMsg.Text = String.Format("Error WFC: {0}", e.Result.Err)
        Exit Try
      End If

      GridData.ItemsSource = e.Result.Contacts

      UpdateStatus("Ws GetData Completed")

    Catch ex As Exception
      txMsg.Text = ex.ToString
    End Try
  End Sub


#End Region

#Region "Buttons Management"

  Private Sub btTest_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btTest.Click
    Try
      InitializeWs(WsService)

      WsService.TestAsync(txSearch.Text)

      UpdateStatus("Ws Test Calling. . . ")

    Catch ex As Exception
      txMsg.Text = ex.ToString
    End Try
  End Sub

  Private Sub btSearch_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btSearch.Click
    Try
      InitializeWs(WsService)

      WsService.GetDataAsync(txSearch.Text)

      UpdateStatus("Ws GetData Calling. . . ")

    Catch ex As Exception
      txMsg.Text = ex.ToString
    End Try
  End Sub


#End Region

#Region "Control Address Management"

  Private Sub chkAddress_Checked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles chkAddress.Checked
    BaseAddress = txAddress.Text
    txAddress.IsEnabled = True
    txAddress.IsReadOnly = False
  End Sub

  Private Sub chkAddress_Unchecked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles chkAddress.Unchecked
    BaseAddress = "http://localhost:2298/ServiceWCF.svc"
    txAddress.Text = BaseAddress
    txAddress.IsEnabled = False
    txAddress.IsReadOnly = True
  End Sub

  Private Sub txAddress_GotFocus(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles txAddress.GotFocus
    txAddress.IsEnabled = True
    txAddress.IsReadOnly = False
  End Sub

  Private Sub txAddress_LostFocus(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles txAddress.LostFocus

    If chkAddress.IsChecked Then BaseAddress = txAddress.Text

    txAddress.IsEnabled = False
    txAddress.IsReadOnly = True
  End Sub


#End Region


End Class